package com.jike.jkgame.data;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.jike.jkgame.JKApp;

public class Preferences {
	private static final String PREF_NAME = "com.jike.jkgame";
	private static final String PREF_SEEION_ID = "session_id";
	
	private static Preferences mInstance;

	SharedPreferences mSp;

	private long mId = -1; // session id

	private Preferences() {
		mSp = JKApp.get().getSharedPreferences(PREF_NAME, 0);
	}

	public static Preferences get() {
		if (mInstance == null) {
			mInstance = new Preferences();
		}
		return mInstance;
	}

	public long getId() {
		if (mId < 0) {
			mId = mSp.getLong(PREF_SEEION_ID, -1);
		}
		return mId;
	}

	public void saveId(long id) {
		mId = id;
		Editor editor = mSp.edit();
		editor.putLong(PREF_SEEION_ID, mId);
		editor.commit();
	}

	public void put(String key, Object value) {
		Editor editor = mSp.edit();
		if (value instanceof Integer) {
			editor.putInt(key, (Integer) value);
		} else if (value instanceof Long) {
			editor.putLong(key, (Long) value);
		} else if (value instanceof Float) {
			editor.putFloat(key, (Float) value);
		} else if (value instanceof Boolean) {
			editor.putBoolean(key, (Boolean) value);
		} else {
			editor.putString(key, value.toString());
		}
		editor.commit();
	}

	public void putInt(String key, int value) {
		Editor editor = mSp.edit();
		editor.putInt(key, value);
		editor.commit();
	}
	
	public void putLong(String key, long value) {
		Editor editor = mSp.edit();
		editor.putLong(key, value);
		editor.commit();
	}
	
	public void putFloat(String key, float value) {
		Editor editor = mSp.edit();
		editor.putFloat(key, value);
		editor.commit();
	}
	
	public void putBoolean(String key, boolean value) {
		Editor editor = mSp.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}
	
	public void putString(String key, String value) {
		Editor editor = mSp.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public int getInt(String key) {
		return mSp.getInt(key, -1);
	}

	public long getLong(String key) {
		return mSp.getLong(key, -1);
	}

	public float getFloat(String key) {
		return mSp.getFloat(key, -1.0f);
	}

	public boolean getBoolean(String key) {
		return mSp.getBoolean(key, false);
	}

	public String getString(String key) {
		return mSp.getString(key, null);
	}

	public static class Keys {
		public static final String UserName = "user_name";
		public static final String Password = "password";

	}

}
